package net.sourceforge.jweb.jstl.fn;

import java.text.ChoiceFormat;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.jweb.util.ExceptionBuilder;


public class FormatUtil {
	private final static Logger LOGGER = LoggerFactory.getLogger(FormatUtil.class);

	private FormatUtil() {
	}

	public static final String format(Format f, Object obj) {
		if (f == null || obj == null)
			return "";
		try {
			return f.format(obj);
		} catch (Exception e) {
			LOGGER.warn("Can not format obj=" + obj + " from " + f);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static final String formatDate(Date f, String format) {
		if (f == null || format == null)
			return "";
		try {
			return new SimpleDateFormat(format).format(f);
		} catch (Exception e) {
			LOGGER.warn("Can not format obj=" + f + " by " + format);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static Object parse(Format f, String source) {
		if (f == null || source == null)
			return null;
		try {
			return f.parseObject(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String transformDate(String source, String sourceFormat, String destFormat) {
		if (sourceFormat == null || source == null || sourceFormat == null)
			return "";
		try {
			DateFormat sourceF = new SimpleDateFormat(sourceFormat);
			DateFormat destF = new SimpleDateFormat(destFormat);
			return destF.format(sourceF.parse(source));
		} catch (Exception e) {
			LOGGER.warn("Can not transform obj=" + source + " from " + sourceFormat + " to " + destFormat);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static String transformDecimal(String source, String sourceFormat, String destFormat) {
		if (sourceFormat == null || source == null || sourceFormat == null)
			return "";
		try {
			NumberFormat sourceF = new DecimalFormat(sourceFormat);
			NumberFormat destF = new DecimalFormat(destFormat);
			return destF.format(sourceF.parse(source));
		} catch (Exception e) {
			LOGGER.warn("Can not transform obj=" + source + " from " + sourceFormat + " to " + destFormat);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static String transformChoice(String source, String sourceFormat, String destFormat) {
		if (sourceFormat == null || source == null || sourceFormat == null)
			return "";
		try {
			ChoiceFormat sourceF = new ChoiceFormat(sourceFormat);
			NumberFormat destF = new ChoiceFormat(destFormat);
			return destF.format(sourceF.parse(source));
		} catch (Exception e) {
			LOGGER.warn("Can not transform obj=" + source + " from " + sourceFormat + " to " + destFormat);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}
	// -------instance constructor and also in java.text getInstance----

	public static Format getDecimalInstance() {
		return new DecimalFormat();
	}

	public static Format getDecimalInstance(String pattern) {
		return new DecimalFormat(pattern);
	}

	public static Format getDecimalInstance(String pattern, DecimalFormatSymbols s) {
		return new DecimalFormat(pattern, s);
	}

	public static DecimalFormatSymbols newDecimalFormatSymbols() {
		return new DecimalFormatSymbols();
	}

	public static DecimalFormatSymbols newDecimalFormatSymbols(Locale l) {
		return new DecimalFormatSymbols(l);
	}

	public static Format getChoiceInstance(double[] limits, String[] formats) {
		return new ChoiceFormat(limits, formats);
	}

	public static Format getChoiceInstance(String newPattern) {
		return new ChoiceFormat(newPattern);
	}

	public static Format getSimpleDateInstance(String newPattern) {
		return new SimpleDateFormat(newPattern);
	}

	public static Format getSimpleDateInstance() {
		return new SimpleDateFormat();
	}

	public static Format getSimpleDateInstance(String pattern, DateFormatSymbols formatSymbols) {
		return new SimpleDateFormat();
	}

	public static Format getSimpleDateInstance(String pattern, Locale locale) {
		return new SimpleDateFormat(pattern, locale);
	}

	public static DateFormatSymbols newDateFormatSymbols() {
		return new DateFormatSymbols();
	}

	public static DateFormatSymbols newDateFormatSymbols(Locale l) {
		return new DateFormatSymbols(l);
	}

	public static Format getMessageInstance(String pattern, Locale locale) {
		return new MessageFormat(pattern, locale);
	}

	public static Format getMessageInstance(String pattern) {
		return new MessageFormat(pattern);
	}

	public static Date parseDate(String source, String format) {
		try {
			return new SimpleDateFormat(format).parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]) {
		System.out.println(transformDate("23:30:00", "hh:mm:ss", "h:mm aaa"));
	}
}
